
##################
###Updated: December 2018
##################

library(survival)
library(Amelia)
library(AER)
library(DataCombine)
library(stargazer)
library(MatchIt)
library(interflex)

setwd("")

surv.df <- read.csv("Morse_FATF_CrimTF.csv", header=TRUE)
ccodes.surv <- unique(surv.df$ccode)

#########################
###Figure 1: Compliance Comparison
#########################
##Note: Plot shows compliance levels at latest FATF evaluation prior to 
##FATF listing process for listed countries (improvements not considered)
##So, for example, Paraguay was evaluated in 2005 and 2008.  This shows
##compliance level as of 2008 evaluation (16).
##Plot of countries included in survival analysis

df <- read.csv("Morse_FATF_DescriptivePlot", header=TRUE)  
df <- df[df$ccode%in%ccodes.surv,]

ccodes <- unique(df$ccode)
listed <- rep(0, 16)
not <- rep(0, 16)
for(i in 1:length(ccodes)){
  temp <- df[df$ccode==ccodes[i],]
  bin <- unique(temp$bin)
  if(temp$listed==1){
    listed[bin] <- listed[bin]+1
  }
  if(temp$listed==0){
    not[bin] <- not[bin]+1
  }
}
listed
not

number1 <- listed
number2 <- not
listing <- c("Not Listed", "Listed")
listing <- rep(listing, 16)
bin <- c(1, 1, 2, 2, 3, 3, 4, 4,
         5, 5, 6, 6, 7, 7, 8, 8,
         9, 9, 10, 10, 11, 11,
         12, 12, 13, 13, 14, 14,
         15, 15, 16, 16)

number <- c(number2[1], 0,
            number2[2], 0,
            number2[3], 0,
            number2[4], 0,
            number2[5], 0,
            number2[6], 0,
            number2[7], 0,
            number2[8], 0,
            number2[9], 0,
            number2[10], number1[10],
            number2[11], number1[11],
            number2[12], number1[12],
            number2[13], number1[13],
            number2[14], number1[14],
            number2[15], number1[15],
            number2[16], number1[16])

temp <- data.frame(listing,
                   number,
                   bin,
                   stringsAsFactors = FALSE)

temp$listing <- factor(temp$listing)

temp$bar_order[temp$listing == "Not Listed"] <- 1
temp$bar_order[temp$listing == "Listed"] <- 2

fill_palette <- c("#191970", "#3399FF" )

library(ggplot2)
p <- ggplot(data=temp, 
            aes(x=bin, 
                y=number, 
                fill=listing)) +
  geom_bar(stat="identity",
           position = "stack")+
  scale_fill_manual(values=fill_palette)+
  labs(x= "Number of Failing Recommendations",
       y= "Number of Countries")+
  theme_minimal()
p+theme(legend.position = "none")

table(df[df$MER_year<2010,]$bin)

#########################
###Figure 2: Compliance over Time
##Descriptive Plot - Criminalization (Listed vs. Non)
##Requires data set of all countries that have criminalized
##(pre and post-2009)
#########################
df <- read.csv("Morse_FATF_AllCrim.csv", header=TRUE)

tot.num <- c()
num1 <- 0
total <- table(df$Year)

x <- rep(0, 180)

##Monthly Number
listed <- unique(surv.df[surv.df$listed==1,]$ccode)
not <- unique(df[!(df$ccode%in%listed), ]$ccode)

list.num <- rep(0, 180)
for(i in 1:length(listed)){
  temp <- df[df$ccode==listed[i], ]
  year <- ifelse(!is.na(temp$Year), temp$Year, 0)
  month <- ifelse(year!=0, temp$Month, 0)
  num <- year-2000
  if(num>0){
    num <- month+(12*(num-1))
    list.num[num] <- list.num[num]+1
  }
}

not.num <- rep(0, 180)
for(i in 1:length(not)){
  temp <- df[df$ccode==not[i], ]
  year <- ifelse(!is.na(temp$Year), temp$Year, 0)
  month <- ifelse(year!=0, temp$Month, 0)
  num <- year-2000
  if(num>0){
    num <- month+(12*(num-1))
    not.num[num] <-  not.num[num]+1
  }
}

tot.list <- rep(0, 180)
tot.not <- rep(0, 180)
num1 <- 0
num2 <- 0
for(i in 1:180){
  tot.list[i] <- num1+list.num[i]
  num1 <- num1 + list.num[i]
  tot.not[i] <- num2+not.num[i]
  num2 <- num2 + not.num[i]
}

tot.list <- tot.list/length(listed)
tot.not <- tot.not/length(not)

par(mar=c(3, 3, 3, 1))
par(mgp=c(2, 1, 0))
plot(tot.not, 
     ylab="Percent of Countries",
     cex.lab=0.8,
     type='l',
     xaxt='n',
     ylim=c(0,1),
     pch=20,
     xlab="Year",
     yaxt='n',
     main="Trends in Criminalization of Terrorist Financing",
     cex.main=0.9)
lines(tot.list, type='l', lty=2, col="red", pch=15)
axis(1, at=c(6, 30, 54, 78, 102, 126, 150, 174),
     lab=c(2001, 2003, 2005, 2007, 2009,
           2011, 2013, 2015), cex.axis=0.8)
axis(2, at=c(0, 0.2, 0.4, 0.6, 0.8, 1),
     lab=c(0, 20, 40, 60, 80, 100), cex.axis=0.8)
abline(v=102, lty=3)
text(74, 0.2, labels="Non-Listed Countries",
     cex=0.7)
text(140, 0.8, labels="Listed Countries",
     cex=0.7)


#########################
###Table 1: Full Sample
#########################

surv.df2 <- surv.df[!is.na(surv.df$usallyc) &
                      !is.na(surv.df$risklistingc) &
                      !is.na(surv.df$diffusion2c) &
                      !is.na(surv.df$crimtfindc),]
length(unique(surv.df2[surv.df2$listed==1,]$ccode))##46
length(unique(surv.df2$ccode))##132
country1 <- sort(unique(surv.df2$country))
cft.cox1 <- coxph(Surv(period, periodnext, crimtfnew) ~
                    listedc+
                    fatfc+
                    crimtfindc+
                    diffusion2c+
                    risklistingc+
                    usallyc+
                    usallyc:log(periodnext)+
                    cluster(ccode),
                  robust=TRUE, data=surv.df)
summary(cft.cox1)

temp <- surv.df2[!is.na(surv.df2$log.creditkc) &
                  !is.na(surv.df2$log.gdppckc) &
                   !is.na(surv.df2$log.liabc),]
length(unique(temp[temp$listed==1,]$ccode))##37
length(unique(temp$ccode))##120
country2 <- sort(unique(temp$country))

cft.cox2 <- coxph(Surv(period, periodnext, crimtfnew) ~
                    listedc*log.liabc+
                    fatfc+
                    crimtfindc+
                    diffusion2c+
                    risklistingc+
                    usallyc+
                    usallyc:log(periodnext)+
                    log.creditkc+
                    log.gdppckc+
                    cluster(ccode),
                  robust=TRUE, data=temp)
summary(cft.cox2)


temp <- surv.df2[!is.na(surv.df2$log.creditkc) &
                   !is.na(surv.df2$log.gdppckc) &
                   !is.na(surv.df2$log.liabc) &
                   !is.na(surv.df2$terrorismc),]
length(unique(temp[temp$listed==1,]$ccode))##32
length(unique(temp$ccode))##96
country3 <- sort(unique(temp$country))

cft.cox3 <- coxph(Surv(period, periodnext, crimtfnew) ~
                    listedc*log.liabc+
                    fatfc+
                    crimtfindc+
                    diffusion2c+
                    risklistingc+
                    usallyc+
                    usallyc:log(periodnext)+
                    log.creditkc+
                    log.gdppckc+
                    terrorismc+
                    cluster(ccode),
                  robust=TRUE, data=temp)
summary(cft.cox3)

temp <- surv.df2[!is.na(surv.df2$log.creditkc) &
                   !is.na(surv.df2$log.gdppckc) &
                   !is.na(surv.df2$log.liabc) &
                   !is.na(surv.df2$terrorismc) &
                   !is.na(surv.df2$newpolityc),]
length(unique(temp[temp$listed==1,]$ccode))##30
length(unique(temp$ccode))##87
country4 <- sort(unique(temp$country))

cft.cox4 <- coxph(Surv(period, periodnext, crimtfnew) ~
                    listedc*log.liabc+
                    fatfc+
                    crimtfindc+
                    diffusion2c+
                    risklistingc+
                    usallyc+
                    usallyc:log(periodnext)+
                    log.creditkc+
                    log.gdppckc+
                    terrorismc+
                    newpolityc+
                    cluster(ccode),
                  robust=TRUE, data=temp)
summary(cft.cox4)

stargazer(cft.cox1, cft.cox2, cft.cox3, cft.cox4,
          apply.coef=exp,
          t.auto=F,
          p.auto=F,
          title="Effect of Non-Complier List on Criminalization",
          align=TRUE,
          covariate.labels=c("Listing",
                             "Market Integration",
                             "FATF Member",
                             "Previous Terrorist Fin Law", 
                             "Diffusion",
                             "Eligible for Listing",
                             "US Ally",
                             "Private Sector Credit", 
                             "Capacity",
                             "Terrorism", 
                             "Democracy",
                             "Listing * Market Integration"),
          column.sep.width="2pt")

##################
###Countries that are dropped from the sample as variables are added
###List provided in Appendix
##################
stargazer(as.vector(country4))
as.vector(country3[!country3%in%country4])
as.vector(country2[!country2%in%country4 &
                     !country2%in%country3])
as.vector(country1[!country1%in%country4 &
                     !country1%in%country3 &
                     !country1%in%country2])

#########################
###Figure 3: Difference in Cumulative Probability
#########################
surv.df$list.liabc <- surv.df$listedc*surv.df$log.liabc
surv.df$usallyc.time <- surv.df$usallyc*log(surv.df$periodnext)

control.1a <- coxph(Surv(period, periodnext, crimtfnew) ~
                      listedc+
                      log.liabc+
                      list.liabc+
                      fatfc+
                      crimtfindc+
                      diffusion2c+
                      risklistingc+
                      usallyc+
                      usallyc.time+
                      log.creditkc+
                      log.gdppckc+
                      terrorismc+
                      newpolityc+
                      cluster(ccode),
                    robust=TRUE, data=surv.df)
summary(control.1a)


sample1a <- complete.cases(surv.df$listedc,
                           surv.df$log.liabc,
                           surv.df$list.liabc,
                           surv.df$crimtfindc,
                           surv.df$fatfc,
                           surv.df$diffusion2c,
                           surv.df$log.creditkc,
                           surv.df$log.gdppckc,
                           surv.df$newpolityc,
                           surv.df$terrorismc)

sample <- cbind(surv.df, sample1a)
consol.sample1a <- subset(sample, sample1a == TRUE)

##Correlation Matrix
s3 <- consol.sample1a
hold3 <- data.frame(s3[,c("listedc", 
                          "log.liabc",
                          "list.liabc",
                          "fatfc",
                          "crimtfindc",
                          "diffusion2c",
                          "risklistingc",
                          "usallyc",
                          "usallyc.time", 
                          "log.creditkc",
                          "log.gdppckc",
                          "terrorismc",
                          "newpolityc")])
round(cor(hold3),3)

datapull <- as.data.frame(consol.sample1a)
nrow(datapull)

listedc <- as.numeric(summary(datapull$listedc))
log.liabc <- as.numeric(summary(datapull$log.liabc))
list.liabc <- as.numeric(summary(datapull$list.liabc))
crimtf.indc <- as.numeric(summary(datapull$crimtfindc))
fatfc <- as.numeric(summary(datapull$fatfc))
diffusion2c <- as.numeric(summary(datapull$diffusion2c))
risklistingc <- as.numeric(summary(datapull$risklistingc))
usallyc <- as.numeric(summary(datapull$usallyc))
usallyc.time <- as.numeric(summary(datapull$usallyc.time))
log.creditkc <- as.numeric(summary(datapull$log.creditkc))
log.gdppckc <- as.numeric(summary(datapull$log.gdppckc))
newpolityc <- as.numeric(summary(datapull$newpolityc))
terrorismc <- as.numeric(summary(datapull$terrorismc))

listup <- max(surv.df$listedc)
logliab.low <- median(surv.df$log.liabc, na.rm=TRUE)-sd(surv.df$log.liabc, na.rm=TRUE)
logliab.up <- median(surv.df$log.liabc, na.rm=TRUE)+sd(surv.df$log.liabc, na.rm=TRUE)
listliab.low <- listup*logliab.low
listliab.up <- listup*logliab.up

listed.surv <- data.frame(
  listedc=c(listup, listup),
  log.liabc=c(logliab.low, logliab.up),
  list.liabc=c(listliab.low, listliab.up),
  fatfc=c(fatfc[1], fatfc[1]),
  crimtfindc=rep(crimtf.indc[3], 2),
  diffusion2c=rep(diffusion2c[4],2),
  risklistingc=rep(risklistingc[4],2),
  usallyc=rep(usallyc[4],2),
  usallyc.time=rep(usallyc.time[4],2),
  log.creditkc=rep(log.creditkc[4],2),
  log.gdppckc=rep(log.gdppckc[4],2),
  newpolityc=rep(newpolityc[4],2),
  terrorismc=rep(terrorismc[4],2)
)

listed.surv <- rbind(listed.surv,
                     listed.surv,
                     listed.surv)

store.listed <- matrix(rep(NA,72000),
                       nrow=72,
                       ncol=500)
set.seed(11)
for(k in 1:500){
  datapull <- as.data.frame(consol.sample1a)
  index <- sample(1:nrow(datapull),5000,replace=TRUE)
  b.data <- consol.sample1a[index,]
  fitcty <- control.1a <- coxph(Surv(period, periodnext, crimtfnew) ~
                                  listedc+
                                  log.liabc+
                                  list.liabc+
                                  fatfc+
                                  crimtfindc+
                                  diffusion2c+
                                  risklistingc+
                                  usallyc+
                                  usallyc.time+
                                  log.creditkc+
                                  log.gdppckc+
                                  terrorismc+
                                  newpolityc+
                                  cluster(ccode),
                                robust=TRUE, data=b.data)
  temp <- summary(survfit(fitcty, newdata=listed.surv))
  d <- data.frame(first=temp$surv[,1],
                  second=temp$surv[,2],
                  time=temp$time)
  d.diff <- d[,2] - d[,1]
  store <- rep(NA, 72)
  for(i in 1:72){
    if (i %in% d$time) {
      store[i] <- d.diff[d$time == i]
    } else if (i == 1) {
      store[1] <- 0
    } else {
      store[i] <- store[i-1]
    }
  }
  store.listed[,k] <- store
}
hold <- store.listed
head(hold)
hold5 <- hold

hold[,1]
hold[,50]


#####Averages and 2.5% and 97.5% observations
ctyest.diff <- ctyci.high <- ctyci.low <- rep(NA,72)
for(i in 1:72){
  ctyest.diff[i] <- mean(as.numeric(hold5[i,2:500]), na.rm=TRUE)
  ctyci.high[i] <- as.numeric(quantile(hold5[i,2:500],
                                       .975, na.rm=TRUE))
  ctyci.low[i] <- as.numeric(quantile(hold5[i,2:500],
                                      .25, na.rm=TRUE))
}

fd2 <- data.frame(ctyest.diff, ctyci.low, ctyci.high)


#######Plot
par(mfrow=c(1,1), mar=(c(5, 6, 4, 2) + 0.1))
par(mar=c(3,4,4,2))
plot(1:40, abs(fd2$ctyest.diff[1:40]), 
     ylim=c(-.01,1),
     xlim=c(0,45),
     ylab="", 
     xlab="",
     type="l", 
     lwd=2,
     axes=F,
     main="")

axis(1)
axis(2,las=2)
abline(h=0)
lines(1:40, abs(fd2$ctyci.high[1:40]), lty=2, lwd=1.5)
lines(1:40, abs(fd2$ctyci.lo[1:40]), lty=2, lwd=1.5)

mtext("Effect of Market Integration and Listing on Criminalization", 
      side=3, line=1,font=2,cex=0.8)
mtext("Cumulative Probability", 
      side=2, line=3, cex=0.8)
mtext("Months", 
      side=1, line=2, cex=0.8)


#########################
###Table 2: Analysis for Matched and Eligible Samples
#########################

#########Matched Sample############
surv.df2 <- surv.df
surv.df2 <- surv.df2[surv.df2$fatf==0,]
surv.df2 <- surv.df2[surv.df2$period==1,]

vars <- c("listed", "listedc", "period", "periodnext",
          "log.liabc", "crimtfindc", "diffusion2c",
          "risklistingc", "usallyc", "log.creditkc",
          "log.gdppckc", "newpolityc", "terrorismc",
          "ccode", "log.aidc")

surv.df2 <- surv.df2[vars]
surv.df2 <- surv.df2[complete.cases(surv.df2),]

m.out1 <- matchit(listed ~ 
                    diffusion2c+
                    usallyc+
                    log.creditkc+
                    log.gdppckc+
                    newpolityc+
                    terrorismc,
                  data = surv.df2, method = "nearest")
summary(m.out1)

m.data <- match.data(m.out1)
ccodes <- unique(m.data$ccode)
treat <- unique(m.data[m.data$listed==1,]$ccode)
control <- unique(m.data[!(m.data$ccode%in%treat), ]$ccode)

length(unique(m.data$ccode))

m.surv <- surv.df[surv.df$ccode%in%ccodes,]
surv <- c()
for(i in 1:length(ccodes)){
  temp <- m.surv[m.surv$ccode==ccodes[i],]
  temp$distance <- m.data[m.data$ccode==ccodes[i],]$distance
  surv <- rbind(surv, temp)
}
table(surv$distance)
table(surv[surv$crimtfnew==1,]$listed)

cft.cox1 <- coxph(Surv(period, periodnext, crimtfnew) ~
                    listedc+
                    crimtfindc+
                    diffusion2c+
                    risklistingc+
                    usallyc+
                    usallyc:log(periodnext)+
                    distance+
                    cluster(ccode),
                  robust=TRUE, data=surv)
summary(cft.cox1)

unique(surv[!is.na(surv$terrorismc) &
              !is.na(surv$newpolityc) &
              !is.na(surv$log.creditkc) &
              !is.na(surv$log.gdppckc) &
              !is.na(surv$log.liabc), ]$ccode)
cft.cox2 <- coxph(Surv(period, periodnext, crimtfnew) ~
                    listedc*log.liabc+
                    crimtfindc+
                    diffusion2c+
                    risklistingc+
                    usallyc+
                    usallyc:log(periodnext)+
                    log.creditkc+
                    log.gdppckc+
                    newpolityc+
                    terrorismc+
                    distance+
                    cluster(ccode),
                  robust=TRUE, data=surv)
summary(cft.cox2)

##############Eligible for listing sample##############

ccodes <- unique(surv.df[surv.df$risklisting==1 &
                           surv.df$period==1,]$ccode)
surv.df2 <- surv.df[surv.df$ccode%in%ccodes,]
surv.df2 <- surv.df2[surv.df2$fatf==0,] ##no FATF members
length(unique(surv.df2$ccode)) ##66
length(unique(surv.df2[surv.df$listed==1,]$ccode)) ##11

table(surv.df2[surv.df2$crimtfnew==1,]$listed)##31 of 36

cft.cox3 <- coxph(Surv(period, periodnext, crimtfnew) ~
                    listedc+
                    crimtfindc+
                    diffusion2c+
                    usallyc+
                    usallyc:log(periodnext)+
                    cluster(ccode),
                  robust=TRUE, data=surv.df2)
summary(cft.cox3)

temp <- surv.df2[!is.na(surv.df2$usallyc) &
                   !is.na(surv.df2$log.creditkc) &
                   !is.na(surv.df2$log.gdppckc) &
                   !is.na(surv.df2$terrorismc) &
                   !is.na(surv.df2$newpolityc),]
length(unique(temp$ccode)) ##39

listed <- unique(temp[temp$listed==1,]$ccode) ##42
table(temp[temp$ccode%in%listed,]$crimtfnew) ##20

cft.cox4 <- coxph(Surv(period, periodnext, crimtfnew) ~
                    listedc*log.liabc+
                    crimtfindc+
                    diffusion2c+
                    usallyc+
                    usallyc:log(periodnext)+
                    log.creditkc+
                    log.gdppckc+
                    newpolityc+
                    terrorismc+
                    cluster(ccode),
                  robust=TRUE, data=surv.df2)
summary(cft.cox4)

stargazer(cft.cox1, cft.cox2, cft.cox3, cft.cox4,
          apply.coef=exp,
          t.auto=F,
          p.auto=F,
          title="Effect of Non-Complier List on Criminalization - Select Sample",
          align=TRUE,
          covariate.labels=c("Listing",
                             "Market Integration",
                             "Previous Terrorist Fin Law", 
                             "Diffusion",
                             "Eligible for Listing",
                             "US Ally",
                             "Private Sector Credit", 
                             "Capacity", 
                             "Polity IV",
                             "Terrorism Risk",
                             "Weights",
                             "Listing * Market Integration",
                             "US Ally * Time"),
          column.sep.width="2pt")


#####################################
####Table 3: US Power Variables
#####################################

temp <- surv.df[!is.na(surv.df$log.liabc) &
                  !is.na(surv.df$usallyc) &
                  !is.na(surv.df$log.creditkc) &
                  !is.na(surv.df$log.gdppckc) &
                  !is.na(surv.df$newpolityc) &
                  !is.na(surv.df$terrorismc) &
                  !is.na(surv.df$log.tradeus.gdpc),]
length(unique(temp$ccode))##87 countries

cft.cox1 <- coxph(Surv(period, periodnext, crimtfnew) ~
                    listedc*log.liabc+
                    fatfc+
                    crimtfindc+
                    diffusion2c+
                    risklistingc+
                    usallyc+
                    usallyc:log(periodnext)+
                    log.creditkc+
                    log.gdppckc+
                    newpolityc+
                    terrorismc+
                    log.tradeus.gdpc+
                    cluster(ccode),
                  robust=TRUE, data=surv.df)
summary(cft.cox1)

temp <- surv.df[!is.na(surv.df$log.liabc) &
                  !is.na(surv.df$usallyc) &
                  !is.na(surv.df$log.creditkc) &
                  !is.na(surv.df$log.gdppckc) &
                  !is.na(surv.df$newpolityc) &
                  !is.na(surv.df$terrorismc) &
                  !is.na(surv.df$log.aidc),]
length(unique(temp$ccode))##87 countries

unique(temp[temp$sanction311==1,]$country)
##Latvia, Lebanon, Bangladesh

cft.cox2 <- coxph(Surv(period, periodnext, crimtfnew) ~
                    listedc*log.liabc+
                    fatfc+
                    crimtfindc+
                    diffusion2c+
                    risklistingc+
                    usallyc+
                    usallyc:log(periodnext)+
                    log.creditkc+
                    log.gdppckc+
                    newpolityc+
                    terrorismc+
                    log.aidc+
                    cluster(ccode),
                  robust=TRUE, data=surv.df)
summary(cft.cox2)

cft.cox3 <- coxph(Surv(period, periodnext, crimtfnew) ~
                    listedc*log.liabc+
                    fatfc+
                    crimtfindc+
                    diffusion2c+
                    risklistingc+
                    usallyc+
                    usallyc:log(periodnext)+
                    log.creditkc+
                    log.gdppckc+
                    newpolityc+
                    terrorismc+
                    sanction311c+
                    cluster(ccode),
                  robust=TRUE, data=surv.df)
summary(cft.cox3)

temp <- surv.df[!is.na(surv.df$log.liabc) &
                  !is.na(surv.df$usallyc) &
                  !is.na(surv.df$log.creditkc) &
                  !is.na(surv.df$log.gdppckc) &
                  !is.na(surv.df$newpolityc) &
                  !is.na(surv.df$terrorismc) &
                  !is.na(surv.df$incsrc),]
length(unique(temp$ccode))##87

cft.cox4 <- coxph(Surv(period, periodnext, crimtfnew) ~
                    listedc*log.liabc+
                    fatfc+
                    crimtfindc+
                    diffusion2c+
                    risklistingc+
                    usallyc+
                    usallyc:log(periodnext)+
                    log.creditkc+
                    log.gdppckc+
                    newpolityc+
                    terrorismc+
                    incsrc+
                    cluster(ccode),
                  robust=TRUE, data=surv.df)
summary(cft.cox4)

stargazer(cft.cox1, cft.cox2, cft.cox3, cft.cox4,
          apply.coef=exp,
          t.auto=F,
          p.auto=F,
          title="Effect of Non-Complier List on Criminalization - Select Sample",
          align=TRUE,
          covariate.labels=c("Listing",
                             "Market Integration",
                             "FATF Member",
                             "Previous Terrorist Fin Law", 
                             "Diffusion",
                             "Eligible for Listing",
                             "US Ally",
                             "Private Sector Credit", 
                             "Capacity", 
                             "Democracy",
                             "Terrorism Risk",
                             "Trade with US (Percent GDP)",
                             "US Foreign Aid",
                             "US - 311 Sanctions List",
                             "US - AML List",
                             "Listing * Market Integration",
                             "US Ally * Period Next"),
          column.sep.width="2pt")

##################################
###Table 4: Bank Analysis
###Note: Results robust to including real exchange rate
###instead of balance of trade
##################################

banks <- read.csv("Morse_FATF_banklending.csv", header=TRUE)

temp <- banks[!is.na(banks$lag.inflation) &
                !is.na(banks$lag.gdpgrowth) &
                !is.na(banks$lag.realexchangerate) &
                !is.na(banks$log.liab) &
                !is.na(banks$lag.listed) &
                !is.na(banks$lag.credit.gdp) &
                !is.na(banks$lag.debt.gdp),]

length(unique(temp[temp$lag.listed==1,]$ccode)) ##12
length(unique(temp$ccode)) ##50

###Note: to use the robust=T command, you need to have code from
###https://economictheoryblog.com/2016/08/07/robust-standard-errors-in-r-function/

fit1 <- lm(log.liab~lag.listed+
             lag.inflation+
             lag.gdpgrowth+
             lag.realexchangerate+
             lag.credit.gdp+
             lag.debt.gdp+
             factor(ccode-1),
           data=temp)
summary(fit1, robust=T)
fit1a <- summary(fit1, robust=T)

fit2 <- lm(log.liab~lag.listed+
             lag.inflation+
             lag.gdpgrowth+
             lag.realexchangerate+
             lag.credit.gdp+
             lag.debt.gdp+
             time+time^2+time^3+
             factor(ccode-1),
           data=temp)
fit2a <- summary(fit2, robust=T)

temp2 <- temp[!is.na(temp$lag.m2) &
                !is.na(temp$lag.intratespread),]
length(unique(temp2[temp2$lag.listed==1,]$ccode)) ##10
length(unique(temp2$ccode)) ##39

fit3 <- lm(log.liab~lag.listed+
             lag.inflation+
             lag.gdpgrowth+
             lag.realexchangerate+
             lag.credit.gdp+
             lag.debt.gdp+
             lag.m2+
             lag.intratespread+
             factor(ccode-1),
           data=temp2)
fit3a <- summary(fit3, robust=T)

fit4 <- lm(log.liab~lag.listed+
             lag.inflation+
             lag.gdpgrowth+
             lag.realexchangerate+
             lag.credit.gdp+
             lag.debt.gdp+
             lag.m2+
             lag.intratespread+
             time+time^2+time^3+
             factor(ccode-1),
           data=temp2)
fit4a <- summary(fit4, robust=T)

stargazer(fit1, fit2, fit3, fit4,
          keep=c("lag.listed", 
                 "lag.inflation",
                 "lag.gdpgrowth", 
                 "lag.realexchangerate",
                 "lag.credit.gdp", 
                 "lag.debt.gdp",
                 "time",
                 "lag.m2",
                 "lag.intratespread"),
          title="The Effect of Listing on Bank-to-Bank Lending",
          align=TRUE,
          covariate.labels=c("Listing",
                             "Inflation",
                             "GDP Growth (Percent Change)",
                             "Real Exchange Rate",
                             "Credit-to-GDP Ratio",
                             "Debt-to-GDP Ratio",
                             "Time",
                             "Money Supply",
                             "Interest Rate Spread"),
          column.sep.width="0.1pt",
          keep.stat="n")


#############################
###Figure 4: Thailand Cross-Border Liabilities
#############################

thai <- read.csv("Morse_Thailand_BankLiabilities.csv", header=TRUE)

par(mar=c(5,5,3,1))
thai$lbs_liabilities
x <- seq(from=1, to=26, by=1)
y <- thai$lbs_liabilities[1:26]
plot(x, y, type='b',
     yaxt='n',
     xaxt='n',
     pch=20,
     xlab="Year",
     cex.lab=0.8,
     ylab="Cross-Border Liabilities \n (US$ - Billions)")
axis(1, at=c(1, 5, 9, 13, 17, 21, 25),
     lab=c(2009, 2010, 2011, 2012, 2013,
           2014, 2015), cex.axis=0.8)
axis(2, at=c(14000, 16000, 18000, 20000),
     lab=c(1.4, 1.6, 1.8, 2.0), cex.axis=0.8)
abline(v=5, lty=3)
abline(v=13, lty=3)
abline(v=18, lty=3)





